import logging

# 此处进行Logging.basicConfig() 设置，后面设置无效
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')


# file_handler = logging.FileHandler("log.txt")  # 文件句柄
# file_handler.setLevel(logging.INFO)
# formatter = logging.Formatter(
#     '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# file_handler.setFormatter(formatter)
# logger.addHandler(file_handler)

# 定义一个RotatingFileHandler，最多备份3个日志文件，每个日志文件最大1K
# rHandler = RotatingFileHandler("log.txt", maxBytes=1 * 1024, backupCount=3)
# rHandler.setLevel(logging.INFO)
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# rHandler.setFormatter(formatter)
# logger.addHandler(rHandler)

# console_handler = logging.StreamHandler()  # 流句柄
# console_handler.setLevel(logging.INFO)
# logger.addHandler(console_handler)

logging.debug('debug，用来打印一些调试信息，级别最低')
logging.info('info，用来打印一些正常的操作信息')
logging.warning('waring，用来用来打印警告信息')
logging.error('error，一般用来打印一些错误信息')
logging.critical('critical，用来打印一些致命的错误信息，等级最高')


logger = logging.getLogger(__name__)
logger.info("Start print log")
logger.debug("Do something")
logger.warning("Something maybe fail.")
logger.info("Finish")

try:
    open("sklearn.txt", "rb")
except Exception:
    logger.error("Faild to open sklearn.txt from logger.error", exc_info=True) 
    #logger.exception(msg,_args)等价于logger.error(msg,exc_info = True,_args)
logger.info("Finish")
